<!-- 选择采购合同 -->
<template>
  <el-dialog
    v-model="dialogFormVisible"
    title="选择采购合同"
    width="70%"
    draggable
    overflow
    @close="handleClose"
  >
    <div class="crossRope" />
    <el-form style="margin-top: 2%">
      <el-row>
        <el-col :span="8">
          <el-form-item label="合同编号" style="width: 95%">
            <el-input
              v-model="searchForm.contractNo"
              placeholder="请输入 合同编号"
              size="small"
              clearable
            />
          </el-form-item>
        </el-col>
        <el-col :span="8">
          <el-form-item label="合同名称" style="width: 95%">
            <el-input
              v-model="searchForm.contractName"
              placeholder="请输入 合同名称"
              size="small"
              clearable
            />
          </el-form-item>
        </el-col>
        <el-col :span="8">
          <el-form-item label="项目名称" style="width: 95%">
            <el-input
              v-model="searchForm.projectName"
              placeholder="请输入 项目名称"
              size="small"
              clearable
            />
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="24" style="text-align: right">
          <el-button
            :icon="Search"
            color="#1E3485"
            size="small"
            @click="
              tablePage.currentPage = 1;
              getList();
            "
            >搜索</el-button
          >
          <el-button :icon="Delete" size="small" @click="resetSearchForm()"
            >清空</el-button
          >
        </el-col>
      </el-row>
    </el-form>
    <vxe-table
      border
      min-height="350"
      :column-config="{ resizable: true }"
      :row-config="{ isHover: true }"
      :data="tableData"
      size="mini"
      :loading="tableLoading"
      :pager-config="tablePage"
      style="margin-top: 2%"
      @radio-change="handleRadioChange"
    >
      <vxe-column type="radio" width="60" align="center" />
      <vxe-column
        v-for="column in tableColumns"
        :key="column.field"
        header-align="center"
        :field="column.field"
        :title="column.title"
        :min-width="column.width"
        :align="column.align"
        show-overflow
      >
        <template
          v-if="column.field === 'paymentCycle'"
          v-slot:default="{ row }"
        >
          <span v-if="row.paymentCycle == '0'">到货结算</span>
          <span v-if="row.paymentCycle == '1'">款清发货</span>
          <span v-if="row.paymentCycle == '2'">月度结算</span>
        </template>
        <template v-else v-slot:default="{ row }">
          {{ row[column.field] }}
        </template>
      </vxe-column>
    </vxe-table>
    <vxe-pager
      v-model:current-page="tablePage.currentPage"
      v-model:page-size="tablePage.pageSize"
      :layouts="[
        'Sizes',
        'PrevJump',
        'PrevPage',
        'Number',
        'NextPage',
        'NextJump',
        'FullJump',
        'Total'
      ]"
      :total="tablePage.total"
      size="mini"
      style="margin-top: 2%"
      @page-change="handlePageChange"
    />
    <div class="crossRope" />
    <div style="text-align: right; margin-top: 2%">
      <el-button
        color="#1E3485"
        :icon="Select"
        size="small"
        @click="selectConfirm"
        >确定</el-button
      >
      <el-button :icon="CloseBold" size="small" @click="handleClose"
        >关闭</el-button
      >
    </div>
  </el-dialog>
</template>

<script setup lang="ts">
import { ref, reactive, watch } from "vue";
import { ElDialog, ElMessage } from "element-plus";
import { Delete, Search, Select, CloseBold } from "@element-plus/icons-vue";
import { VxePagerEvents } from "vxe-table";
import { getProcureContractsList } from "@/api/ERP_procure";

// 弹窗关闭/打开事件
const props = defineProps({
  visible: Boolean
});
const dialogFormVisible = ref(false);
watch(
  () => props.visible,
  val => {
    dialogFormVisible.value = val;
  },
  { immediate: true }
);
const emit = defineEmits(["close", "confirm"]);
function handleClose() {
  emit("close", false);
}

// 搜索表单
let searchForm = reactive({
  contractNo: "",
  contractName: "",
  projectName: ""
});
// 表格数据
let tableData = ref([]);
let tableLoading = ref(false);
const selectedRow = ref(null as any);
const tableColumns = reactive([
  {
    field: "contractNo",
    title: "合同编号",
    width: 180,
    align: "left"
  },
  {
    field: "contractName",
    title: "采购合同名称",
    width: 200,
    align: "left"
  },
  {
    field: "projectName",
    title: "项目名称",
    width: 200,
    align: "left"
  },
  {
    field: "contractTotalPrice",
    title: "合同总额（元）",
    width: 120,
    align: "right"
  },
  {
    field: "paymentCycle",
    title: "付款周期",
    width: 120,
    align: "center"
  },
  {
    field: "projectDeptName",
    title: "所属公司",
    width: 200,
    align: "left"
  },
  {
    field: "supplierDeptName",
    title: "生产厂家（供应商）",
    width: 200,
    align: "left"
  }
]);
// 表格分页配置
const tablePage = reactive({
  total: 0,
  currentPage: 1,
  pageSize: 10
});
// 表格分页事件
const handlePageChange: VxePagerEvents.PageChange = ({
  currentPage,
  pageSize
}) => {
  tablePage.currentPage = currentPage;
  tablePage.pageSize = pageSize;
  getList();
};
// 表格选中行
const handleRadioChange = event => {
  selectedRow.value = event.row; // 更新选中行的数据
};
// 清空按钮
function resetSearchForm() {
  searchForm.contractNo = "";
  searchForm.contractName = "";
  searchForm.projectName = "";
}
// 获取列表
async function getList() {
  tableLoading.value = true;
  let params: ApiParams = {};
  params.pageIndex = tablePage.currentPage;
  params.pageSize = tablePage.pageSize;
  params.auditStatus = 2;
  if (searchForm.contractNo) params.contractNo = searchForm.contractNo;
  if (searchForm.contractName) params.contractName = searchForm.contractName;
  if (searchForm.projectName) params.projectName = searchForm.projectName;
  try {
    const response: ApiResponse = await getProcureContractsList(params);
    if (response.code === 200) {
      tableData.value = response.data.records;
      tablePage.total = response.data.totalElements;
    }
  } catch (error) {
    ElMessage.error("获取表格数据失败");
  } finally {
    tableLoading.value = false;
  }
}
// 确认按钮
function selectConfirm() {
  if (selectedRow.value.length === 0) {
    ElMessage.error("请选择一条数据");
  } else {
    emit("confirm", {
      contractId: selectedRow.value.id,
      contractNo: selectedRow.value.contractNo,
      contractName: selectedRow.value.contractName,
      saleContractId: selectedRow.value.saleContractId,
      purchaseDeptId: selectedRow.value.purchaseDeptId,
      purchaseDeptName: selectedRow.value.purchaseDeptName,
      projectId: selectedRow.value.projectId,
      projectName: selectedRow.value.projectName,
      paymentCycle: selectedRow.value.paymentCycle
    });
    // 关闭弹窗
    handleClose();
  }
}

// 弹窗打开时触发
watch(dialogFormVisible, newValue => {
  if (newValue) {
    getList();
  }
});
</script>

<style lang="scss" scoped></style>
